package com.example.news.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.news.bean.TypeDb;
import com.example.news.bean.TypeJson;
import com.example.news.db.TypeSqliteHelper;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/5/21.
 */
public class TypeDao {

    private TypeSqliteHelper helper;
    private SQLiteDatabase db;

    public TypeDao(Context context){
        helper = new TypeSqliteHelper(context);
        db = helper.getWritableDatabase();
    }

    /**
     * 定义添加数据的方法
     */
    public void add(ArrayList<TypeJson.MyData> list){
        for (TypeJson.MyData m:list) {
            ContentValues values=new ContentValues();
            values.put("_id",m.id);
            values.put("name",m.name);
            values.put("url",m.image);
            db.insert("types",null,values);
        }
    }
    /**
     * 定义查询的方法
     */
    public ArrayList<TypeDb> query(){
        ArrayList<TypeDb> list=new ArrayList<>();
        Cursor cursor = db.query("types", null, null, null, null, null, null);
        while(cursor.moveToNext()){

            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String url = cursor.getString(cursor.getColumnIndex("url"));

            list.add(new TypeDb(id,name,url));
        }
        return list;
    }
    /**
     * 定义删除的方法
     */
    public void del(){
        db.delete("types",null,null);
    }
}
